from rest_framework_jwt.authentication import jwt_decode_handler

from users.models import User


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    """
    return {
        'status': 0,
        'message': '登陆成功',
        'token': 'Bearer '+token,
    }


def get_user(request):
        # 前端路由信息未携带用户信息  通过token获取用户信息
        header_token = request.META.get("HTTP_AUTHORIZATION")
        token = header_token.split(' ', 1)[1]
        username = jwt_decode_handler(token).get('username')
        user = User.objects.get(username=username)
        return user